package org.rcgwt.client.ui.calendar.event;

import org.rcgwt.client.ui.calendar.controller.CalendarAction;
import org.rcgwt.client.ui.calendar.controller.CalendarController;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.dom.client.MouseWheelEvent;
import com.google.gwt.event.dom.client.MouseWheelHandler;

public class ExtraEffectsController implements MouseOverHandler,
		MouseOutHandler, MouseWheelHandler, MouseDownHandler, MouseUpHandler {

	private boolean isClicked;
	private CalendarController controller;

	private int elementX;

	public ExtraEffectsController(final CalendarController controller) {
		this.controller = controller;
		isClicked = false;
	}

	@Override
	public void onMouseOver(MouseOverEvent event) {
		elementX = event.getClientX();
	}

	@Override
	public void onMouseOut(MouseOutEvent event) {
		if (isClicked == true) {
			int x = event.getClientX();
			if (x < elementX) {
				controller.handle(CalendarAction.PREVIOUS_MONTH);
			} else if (x > elementX) {
				controller.handle(CalendarAction.NEXT_MONTH);
			}
			isClicked = false;
		}
	}

	@Override
	public void onMouseWheel(MouseWheelEvent event) {
		event.preventDefault();
		if (event.isShiftKeyDown() == true) {
			controller.changeContextCursor();
			return;
		}
		if (event.isControlKeyDown() == true) {
			// VIEW Change (TODO)
			return;
		}
	}

	@Override
	public void onMouseDown(MouseDownEvent event) {
		isClicked = true;
	}

	@Override
	public void onMouseUp(MouseUpEvent event) {
		isClicked = false;
	}
}
